package com.lgx.mall.basic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.basic.dao.IFareDao;
import com.lgx.mall.basic.entity.Fare;
import com.lgx.mall.basic.service.IFareService;

@Service
public class FareService implements IFareService {

	@Autowired
	private IFareDao fareDao;
	
	public IFareDao getFareDao() {
		return fareDao;
	}

	public void setFareDao(IFareDao fareDao) {
		this.fareDao = fareDao;
	}

	@Override
	public Fare createFare(Fare fare) {
		try {
			return this.fareDao.createFare(fare);
		} catch (DataException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Fare updateFare(Fare fare) {
		try {
			return this.fareDao.updateFare(fare);
		} catch (DataException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void deleteFare(Fare fare) {
		try {
			this.fareDao.deleteFare(fare);
		} catch (DataException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteFareById(long id) {
		try {
			this.fareDao.deleteFareById(id);
		} catch (DataException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteFare(String[] ids) {
		try {
			this.fareDao.deleteFare(ids);
		} catch (DataException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Fare findFareById(long id) {
		return this.fareDao.findFareById(id);
	}

	@Override
	public List<Fare> findFareList() {
		return this.fareDao.findFareList();
	}

	@Override
	public Pagination findFarePagination(String fromCity, String toCity,
			int pageSize, int pageNum) {
		return this.fareDao.findFarePagination(fromCity, toCity, pageSize, pageNum);
	}

	@Override
	public double calculationOfShippingCosts(String fromCity, String toProvince,String toCity,String toRegion,double weight,int amount) {
		List<Fare> fares = this.fareDao.findFareList(fromCity,toProvince,toCity,toRegion);
		
		//默认配置
		double firstWeight = 1;//首重1公斤
		double firstPrice = 8;//首重8元
		
		double nextWeight = 1;//续重1公斤
		double nextPrice = 2;//续重2元
		
		if(fares.size() == 0){

			//首重
			if(firstWeight >= weight){
				return firstPrice;
			}
			
			//续重
			if(firstWeight < weight){
				double next = weight - firstWeight;
				double num = Math.ceil(next/nextWeight);
				return firstPrice + num*nextPrice;
			}
		}
		
		Fare fare = fares.get(0);
		
		if(fare.getFirstWeight() == 0){
			fare.setFirstWeight(firstWeight);
		}
		
		if(fare.getFirstPrice() == 0){
			fare.setFirstPrice(firstPrice);
		}
		
		if(fare.getNextWeight() == 0){
			fare.setFirstWeight(nextWeight);
		}
		
		if(fare.getNextPrice() == 0){
			fare.setNextPrice(nextPrice);
		}
		
		//首重
		if(fare.getFirstWeight() >= weight){
			return fare.getFirstPrice();
		}
		
		//续重
		if(fare.getFirstWeight() < weight){
			double next = weight - fare.getFirstWeight();
			double num = Math.ceil(next/fare.getNextWeight());
			return fare.getFirstPrice() + num*fare.getNextPrice();
		}
		
		return 0;
	}

}
